package com.dempsey.order.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dempsey.api.RemoteGoodService;
import com.dempsey.api.model.LoginUser;
import com.dempsey.common.core.domain.AjaxResult;
import com.dempsey.common.core.domain.R;
import com.dempsey.common.security.utils.SecurityUtils;
import com.dempsey.order.dao.Order;
import com.dempsey.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * The type Order controller.
 *
 * @author Dempsey Date 2022/5/2 Description
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final static Log log = LogFactory.get();

    @Autowired
    private OrderService orderService;

    @Autowired
    private RemoteGoodService remoteGoodService;


    @PostMapping("/getOrderById")
    public AjaxResult getOrderById(@RequestParam int orderId) {
        log.info("请求参数: {}", orderId);
        Order order = orderService.getById(orderId);
        return AjaxResult.success(order);
    }

    /**
     * 获取用户的订单列表
     *
     * @return the order list by user id
     */
    @PostMapping("/getOrderListByUserId")
    public AjaxResult getOrderListByUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Integer userId = loginUser.getUser().getUserId();

        QueryWrapper<Order> qw = new QueryWrapper<>();
        if ("user".equals(loginUser.getRole())){
            qw.eq("consumer_id", userId);
        }
        if ("merchant".equals(loginUser.getRole())){
            qw.eq("merchant_id", userId);
        }
        List<Order> orderList = orderService.list(qw);
        return AjaxResult.success(orderList);
    }

    /**
     * 获取用户的订单列表
     *
     * @param userId the user id
     * @return the order list by user id
     */
    @PostMapping("/getOrderListByConsumerId")
    public AjaxResult getOrderListByConsumerId(@RequestParam int userId) {
        log.info("请求参数: {}", userId);
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("consumer_id", userId);
        List<Order> orderList = orderService.list(qw);
        return AjaxResult.success(orderList);
    }


    /**
     * 获取商家的订单列表
     *
     * @param userId the user id
     * @return the order list by merchant user id
     */
    @PostMapping("/getOrderListByMerchantUserId")
    public AjaxResult getOrderListByMerchantUserId(@RequestParam int userId) {
        log.info("请求参数: {}", userId);
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("merchant_id", userId);
        List<Order> orderList = orderService.list(qw);
        return AjaxResult.success(orderList);
    }

    // 按需求来说，其实要根据：商品、状态 进行筛选，以生成时间降序的，要写很多接口，这里先不写


    /**
     * Create order ajax result.
     *
     * @param order the order
     * @return the ajax result
     */
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody Order order) {
        log.info("请求参数: {}", order);

        // 1、根据商品id查询库存是否充足
        R<Integer> result = remoteGoodService.getStock(order.getGoodId());
        if (result.getData() > 0) {
            orderService.save(order);

            return AjaxResult.success();
        }
        return AjaxResult.error("库存不足");
    }

    @PostMapping("/updateOrder")
    public AjaxResult updateOrder(@RequestBody Order order) {
        log.info("请求参数: {}", order);

        if (null != order.getStatus() && order.getStatus().equals(4)) {
            order.setEndTime(new Date());
        }

        orderService.updateById(order);
        return AjaxResult.success();
    }


}
